package com.dic.cvt;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dic.bean.BingWordBookExportXml;
import com.dic.bean.Item;
import com.dic.bean.Phrase;
import com.dic.bean.YdWordBookExportXml;
import com.thoughtworks.xstream.XStream;

public class Main {

	public static void main(String[] args) {
//		XStream xstream = new XStream();
//		xstream.processAnnotations(BingWordBookExportXml.class);
//		File file = new File("/home/eternity-home/Desktop/我的生词本20140207.xml");
//		BingWordBookExportXml bingXml = (BingWordBookExportXml)xstream.fromXML(file);
//		System.out.println(bingXml.getPhrases().get(0).getDate());
		
//		xstream.processAnnotations(YdWordBookExportXml.class);
//		File file = new File("/home/eternity-home/Desktop/youdaoword.xml");
//		YdWordBookExportXml ydXml = (YdWordBookExportXml)xstream.fromXML(file);
//		System.out.println(ydXml.getItems().get(0).getWord());
		ydXml2BingXml("/home/eternity-home/Desktop/youdaoword.xml","/home/eternity-home/Desktop/youdaoword2bing.xml");
	}
	
	
	public static void ydXml2BingXml(String ydExpFilePath,String bingExpFilePath){
		File ydExpXml = new File(ydExpFilePath);
		XStream xstream = new XStream();
		xstream.processAnnotations(YdWordBookExportXml.class);
		YdWordBookExportXml ydXml = (YdWordBookExportXml)xstream.fromXML(ydExpXml);
		BingWordBookExportXml bingXml = new BingWordBookExportXml();
		List<Phrase> phrases = new ArrayList<Phrase>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (Item tempItem : ydXml.getItems()) {
			Phrase tempPhrase = new Phrase();
			tempPhrase.setDate(sdf.format(new Date()));
			tempPhrase.setEng(tempItem.getWord());
			tempPhrase.setNote(" ");
			tempPhrase.setDefi(tempItem.getTrans());
			tempPhrase.setPhonetic(tempItem.getPhonetic());
			phrases.add(tempPhrase);
		}
		bingXml.setPhrases(phrases);
		try {
			
			FileOutputStream bingExpXmlOs = new FileOutputStream(new File(bingExpFilePath));
			xstream.processAnnotations(BingWordBookExportXml.class);
			xstream.toXML(bingXml, bingExpXmlOs);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
